Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@glimmer/di

Package Overview
Dependencies
Maintainers
10
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@glimmer/di

Dependency injection support for Glimmer applications.

  • 0.1.11
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
122K
increased by1.57%
Maintainers
10
Weekly downloads
 
Created

What is @glimmer/di?

@glimmer/di is a dependency injection library designed for use with Glimmer.js applications. It allows developers to manage dependencies in a clean and modular way, promoting better code organization and easier testing.

What are @glimmer/di's main functionalities?

Registering Services

This feature allows you to register services within a registry and then look them up from a container. This is useful for managing dependencies and ensuring that services are only instantiated when needed.

const { Container, Registry } = require('@glimmer/di');

class MyService {
  constructor() {
    this.name = 'MyService';
  }
}

const registry = new Registry();
registry.register('service:my-service', MyService);

const container = new Container(registry);
const myService = container.lookup('service:my-service');
console.log(myService.name); // Output: MyService

Injecting Dependencies

This feature allows you to inject dependencies into your services. By registering injections, you can ensure that services receive the dependencies they need without having to manually pass them around.

const { Container, Registry, inject } = require('@glimmer/di');

class Logger {
  log(message) {
    console.log(message);
  }
}

class MyService {
  constructor(logger) {
    this.logger = logger;
  }

  doSomething() {
    this.logger.log('Doing something!');
  }
}

const registry = new Registry();
registry.register('service:logger', Logger);
registry.register('service:my-service', MyService);
registry.registerInjection('service:my-service', 'logger', 'service:logger');

const container = new Container(registry);
const myService = container.lookup('service:my-service');
myService.doSomething(); // Output: Doing something!

Singleton Services

This feature allows you to register services as singletons, ensuring that only one instance of the service exists within the container. This is useful for services that maintain state or need to be shared across different parts of the application.

const { Container, Registry } = require('@glimmer/di');

class SingletonService {
  constructor() {
    this.counter = 0;
  }

  increment() {
    this.counter++;
  }
}

const registry = new Registry();
registry.register('service:singleton', SingletonService, { singleton: true });

const container = new Container(registry);
const singleton1 = container.lookup('service:singleton');
const singleton2 = container.lookup('service:singleton');
singleton1.increment();
console.log(singleton2.counter); // Output: 1

Other packages similar to @glimmer/di

FAQs

Package last updated on 16 Mar 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc